﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ServiceModel;

namespace PJ.Data.WCF
{
    /// <summary>
    /// ObjectProviderClient : ClientBase object for WCF services.
    /// </summary>
    /// <typeparam name="S"></typeparam>
    /// <typeparam name="D">Must be a concrete object. Interfaces and Abstract objects are not supported by WCF</typeparam>
    public abstract class ObjectProviderClient<S, D> : ClientBase<S>, IObjectWCFProvider<D>, IDisposable
        where D : BaseObjectData, new()
        where S : class, IObjectWCFProvider<D>
    {

        protected ObjectProviderClient()
            : base(typeof(S).Name)
        {
        }

        protected ObjectProviderClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }

        protected ObjectProviderClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        protected ObjectProviderClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        protected ObjectProviderClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }


        public D GetData(string sessionId, string globalId, int mode)
        {
            if (globalId == null)
                throw new ArgumentNullException();
            return base.Channel.GetData(sessionId, globalId, mode);
        }

        public List<D> GetList(string sessionId, string[] globalIds, int mode)
        {
            if (globalIds == null)
                throw new ArgumentNullException();
            return base.Channel.GetList(sessionId, globalIds, mode);
        }


        public bool Exist(string sessionId, string globalId)
        {
            if (globalId == null)
                throw new ArgumentNullException();
            return base.Channel.Exist(sessionId, globalId);
        }

        public void Delete(string sessionId, string globalId)
        {
            if (globalId == null)
                throw new ArgumentNullException();
            base.Channel.Delete(sessionId, globalId);
        }

        public void Persist(string sessionId, D data)
        {
            if (data == null)
                throw new ArgumentNullException();
            base.Channel.Persist(sessionId, data);
        }
        public D PersistAndGet(string sessionId, D data)
        {
            if (data == null)
                throw new ArgumentNullException();
            return base.Channel.PersistAndGet(sessionId, data);
        }
        public void Dispose()
        {
            if (State == CommunicationState.Faulted)
                Abort();
            else
                Close();
        }

        public D CreateNewItem()
        {
            return new D();
        }



        #region Interface override


        #endregion     

    }
}
